package com.game.pacman.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class PlayMapScreen extends JPanel implements Runnable{
	private Image wallImage;
	private Image smallitemImage;
	private Image bigitemImage;
	public final int CELL_WIDTH = 25;
	public final int CELL_HEIGHT = 25; 
	// 충돌 체크를 위해 사용하는 영역 객체
	private Rectangle playerRect = new Rectangle();// 충돌체크 할 때 사용
	private Rectangle smallitemRect = new Rectangle();// 충돌체크 할 때 사용
	//작은아이템 (삭제가 되기때문에 자료형을 LinkedList<int [][]>로 하였음)
	public LinkedList<int [][]> smallItemList = new LinkedList<int [][]>();
	//큰아이템 (삭제가 되기때문에 자료형을 LinkedList<int [][]>로 하였음)
	public LinkedList<int [][]> bigItemList = new LinkedList<int [][]>();
	//벽리스트 (삭제가 되기 않기 때문에 자료형을 ArrayList<int [][]>로 하였음)
	public ArrayList<int [][]> wallList = new ArrayList<int[][]>();
	public Player player = new Player();
	public PlayMapScreen(){
		setLayout(null);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);
		//setPreferredSize(new Dimension(Constants.WINDOW_WIDTH,Constants.WINDOW_HEIGHT));
		setBounds(Constants.BUTTON_WIDTH,0,Constants.MAP_WIDTH,Constants.MAP_HEIGHT);
		
		try{
			URL url = getClass().getResource("/wall.jpg");
			Image originalImage = ImageIO.read(url);
			wallImage = originalImage.getScaledInstance(CELL_WIDTH, CELL_HEIGHT,Image.SCALE_SMOOTH);
			url = getClass().getResource("/smallitem.png");
			originalImage = ImageIO.read(url);
			smallitemImage = originalImage.getScaledInstance(CELL_WIDTH, CELL_HEIGHT,Image.SCALE_SMOOTH);
			url = getClass().getResource("/bigitem.png");
			originalImage = ImageIO.read(url);
			bigitemImage = originalImage.getScaledInstance(CELL_WIDTH, CELL_HEIGHT,Image.SCALE_SMOOTH);
		}catch(Exception e){
			
		}
//		startGame();
	}
	
	public void startGame(){
		startBigItem();
		Thread thread = new Thread(this);
		thread.start();
	}
	public void startBigItem(){
		for(int y = 0; y<=28; y++){
			for(int x = 0; x<=25; x++){
				setList(y,x);
			}
		}
	}
	/**
	 * x,y 좌표값을 받아서 벽,큰아이템,작은아이템 리스트 추가 해주는 메서드 
	 * @param x,y
	 * @since 2013.09-29
	 */
	public void setList(int y, int x){
		int [][] item = {{x,y}};
		if(y == 0 || y==19){
			if(x==12||x==13){
				wallList.add(item);
			}else{
				if((x==0||x==25)&&y==19){
					bigItemList.add(item);
				}else{
					if(!((x==0||x==25)&&y==0)){
						smallItemList.add(item);
					}
				}
			}
		}else if(y == 1||y==2|| y==3){
			if(x==1||x==2||x==3||x==4||x==6||x==7||x==8||x==9||x==10||x==12||x==13||x==15||x==16||x==17||
					x==18||x==19||x==21||x==22||x==23||x==24){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y ==5 || y==6){
			if(x==1||x==2||x==3||x==4||x==6||x==7||x==9||x==10||x==11||x==12||x==13||x==14||x==15||x==16
					||x==18||x==19||x==21||x==22||x==23||x==24){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y == 7){
			if(x==6 || x==7 || x==12 || x==13 || x==18 || x==19){
				wallList.add(item);
			}else{
				if(x==0||x==25){
					bigItemList.add(item);
				}else{
					smallItemList.add(item);
				}
			}
		}else if(y==8 || y==9){
			if(x==0 || x==1 || x==2 || x==3 || x==4 || x==6 || x==7 || x==8 || x==9 || x==10 || x==12
					|| x==13 || x==15 || x==16 || x==17 || x==18 || x==19 || x==21 || x==22 || x==23 ||
					x==24 || x==25){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y == 10 || y == 11 || y == 12||y ==14 || y==15 || y==16){
			if(x==0 || x==1 || x==2 || x==3 || x==4 || x==6||x==7 || x==18 || x==19 || x==21 || x==22 
					|| x==23|| x==24 || x==25){
				wallList.add(item);
			}else{
				if(!(x==8||x==9||x==10||x==11||x==12||x==13||x==14||x==15||x==16||x==17)){
					smallItemList.add(item);
				}
			}
		}else if(y == 13){
			if(x==0 || x==1 || x==2 || x==3 || x==4 || x==21||x==22 || x==23 || x==24 || x==25){
				wallList.add(item);
			}else{
				if(!(x==8||x==9||x==10||x==11||x==12||x==13||x==14||x==15||x==16||x==17)){
					smallItemList.add(item);
				}
			}	
		}else if(y == 17 || y==18){
			if(x==0 || x==1 || x==2 || x==3 || x==4 || x==6||x==7 || x==9 || x==10 || x==11||x==12 
					|| x==13 || x==14 || x==15||x==16 || x==18 || x==19 || x==21 || x==22 || x==23
					||x==24 || x==25){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y==20 || y==21||y == 26 || y==27){
			if(x==1 || x==2 || x==3 || x==4 || x==5 || x==6||x==7 || x==8 || x==9 || x==10||x==12 
					|| x==13 || x==15 || x==16||x==17 || x==18 || x==19 || x==21 || x==22 || x==23
					||x==24){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y==22){
			if(x==3 || x==4 || x==21 || x==22){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y == 23 || y == 24){
			if(x==0 || x==1 || x==3 || x==4 || x==6 || x==7||x==9 || x==10 || x==11 || x==12||x==13 
					|| x==14 || x==15 || x==16||x==18 || x==19 || x==21 || x==22 || x==24 || x==25){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else if(y == 25){
			if(x==6 || x==7 || x==12 || x==13 || x==18 || x==19){
				wallList.add(item);
			}else{
				smallItemList.add(item);
			}
		}else{
			smallItemList.add(item);
		}
				
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			System.out.println(player.getX());
			System.out.println(player.getY());
			System.out.println("-------------------");
			for(int i = 0; i<smallItemList.size(); i++){
				int [][] item = smallItemList.get(i);
				//플레이어 업데이트 해줘야함
				smallitemRect.height = CELL_HEIGHT;
				smallitemRect.width = CELL_WIDTH;
				smallitemRect.x = item[0][1];
				smallitemRect.y = item[0][0];
				playerRect.x = player.getX();
				playerRect.y = player.getY();
				playerRect.width = player.getWidth();
				playerRect.height = player.getHeight();
				if(smallitemRect.intersects(playerRect)){
					System.out.println("충돌 충돌");
				}
				
			}
			player.update();
		}
	}
	@Override
	public void paint(Graphics arg0) {
		// TODO Auto-generated method stub
		super.paint(arg0);
		
		for(int i = 0; i<wallList.size();i++){
			int [][] item = wallList.get(i);
			arg0.drawImage(wallImage, 
			item[0][0]*CELL_WIDTH, item[0][1]*CELL_WIDTH, 
			CELL_WIDTH, CELL_HEIGHT, this);
		}
		for(int i = 0; i<smallItemList.size();i++){
			int [][] item = smallItemList.get(i);
			arg0.drawImage(smallitemImage, 
					item[0][0]*CELL_WIDTH, item[0][1]*CELL_WIDTH, 
					CELL_WIDTH, CELL_HEIGHT, this);
		}
		for(int i = 0; i<bigItemList.size();i++){
			int [][] item = bigItemList.get(i);
			arg0.drawImage(bigitemImage, 
					item[0][0]*CELL_WIDTH, item[0][1]*CELL_WIDTH, 
					CELL_WIDTH, CELL_HEIGHT, this);
		}		
	}

	public ArrayList<int[][]> getWallList() {
		return wallList;
	}

	public void setWallList(ArrayList<int[][]> wallList) {
		this.wallList = wallList;
	}
}
